package comu.hnevc.zh.testdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.List;

import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.bean.Light;
import comu.hnevc.zh.testdemo.dialog.TrafficlightDialog;

public class TrafficlightAdapter extends BaseAdapter {
    Context mContext;
    List<Light> mLightlist;

    public TrafficlightAdapter(Context mContext, List<Light> mLightlist) {
        this.mContext = mContext;
        this.mLightlist = mLightlist;
    }

    @Override
    public int getCount() {
        return mLightlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mLightlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Light light=mLightlist.get(position);
         convertView=LayoutInflater.from(mContext).inflate(R.layout.item_traffic_list,null);
        TextView roadum=convertView.findViewById(R.id.tv_trafficdialog_roadnum);
        TextView redtime=convertView.findViewById(R.id.tv_trafficdialog_redtime);
        TextView greentime=convertView.findViewById(R.id.tv_trafficdialog_greentime);
        TextView yellowtime=convertView.findViewById(R.id.tv_trafficdialog_yellowtime);
        CheckBox cb=convertView.findViewById(R.id.cb_trafficdialog);
        Button bt=convertView.findViewById(R.id.bt_trafficdialog);
        roadum.setText(light.getRoadnum()+"");
        redtime.setText(light.getRedtime()+"");
        greentime.setText(light.getGreentime()+"");
        yellowtime.setText(light.getYellowtime()+"");
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                light.setNeedcharge(true);
            }
        });
             bt.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           for (int i=0;i<mLightlist.size();i++){
               Light light1=mLightlist.get(i);
               light1.setNeedcharge(false);
           }
           light.setNeedcharge(true);
           TrafficlightDialog dialog=new TrafficlightDialog(mContext,mLightlist);
           dialog.show();
       }
   });
        return convertView;
    }
}
